package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import ym.mapper.FoodItemMapper;
import ym.mapper.MealMapper;
import ym.pojo.FoodItem;
import ym.pojo.Meal;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class MealServiceImpl implements MealService {
    @Autowired
    private MealMapper mealMapper;
    @Autowired
    private FoodItemMapper foodItemMapper;

    @Override
    public void add(Meal meal) {
        mealMapper.insert(meal);
    }

    @Override
    public void update(Meal meal) {
        mealMapper.updateById(meal);
    }

    @Override
    public void delete(Integer id) {
        mealMapper.deleteById(id);
    }

    @Override
    public Meal findById(Integer id) {
        return mealMapper.selectById(id);
    }

    @Override
    public List<Meal> findAll() {
        return mealMapper.selectList(null);
    }

    @Override
    public Page<Meal> search(String search, int page, int size) {
        QueryWrapper<Meal> queryWrapper = new QueryWrapper<>();
        if (search != null) {

        }
        //加入餐品项
        List<Meal> mealList1 = new ArrayList<>();
        Page<Meal> mealPage = mealMapper.selectPage(new Page(page, size), queryWrapper);
        List<Meal> mealList2 = mealPage.getRecords();
        for (Meal meal : mealList2) {
            QueryWrapper<FoodItem> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("mealId", meal.getMealId());
            List<FoodItem> foodItemList = foodItemMapper.selectList(queryWrapper1);
            meal.setFoodItems(foodItemList);
            mealList1.add(meal);
        }
        mealPage.setRecords(mealList1);
        return mealPage;
    }
}
